package com.sm.chatinputlibrary.emoji;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import android.text.style.DynamicDrawableSpan;

/**
 * 表情居中,并设置大小
 * 参考
 * {link https://github.com/vanniktech/Emoji/blob/master/emoji/src/main/java/com/vanniktech/emoji/EmojiSpan.java}
 */
public class EmojiSpan extends DynamicDrawableSpan {
    private final float size;
    private final Context context;
    private final EmojiBean emoji;
    private Drawable deferredDrawable;

    EmojiSpan(final Context context, final EmojiBean emoji, final float size) {
        this.context = context;
        this.emoji = emoji;
        this.size = size;
    }

    @Override
    public Drawable getDrawable() {
        if (deferredDrawable == null) {
            deferredDrawable = emoji.getDrawable(context);
            deferredDrawable.setBounds(0, 0, (int) size, (int) size);
        }
        return deferredDrawable;
    }

    @Override
    public int getSize(@NonNull final Paint paint, final CharSequence text, final int start,
                       final int end, final Paint.FontMetricsInt fontMetrics) {
        if (fontMetrics != null) {
            final Paint.FontMetrics paintFontMetrics = paint.getFontMetrics();
            final float fontHeight = paintFontMetrics.descent - paintFontMetrics.ascent;
            final float centerY = paintFontMetrics.ascent + fontHeight / 2;

            fontMetrics.ascent = (int) (centerY - size / 2);
            fontMetrics.top = fontMetrics.ascent;
            fontMetrics.bottom = (int) (centerY + size / 2);
            fontMetrics.descent = fontMetrics.bottom;
        }

        return (int) size;
    }

    @Override
    public void draw(@NonNull final Canvas canvas, final CharSequence text, final int start,
                     final int end, final float x, final int top, final int y,
                     final int bottom, @NonNull final Paint paint) {
        final Drawable drawable = getDrawable();
        final Paint.FontMetrics paintFontMetrics = paint.getFontMetrics();
        final float fontHeight = paintFontMetrics.descent - paintFontMetrics.ascent;
        final float centerY = y + paintFontMetrics.descent - fontHeight / 2;
        final float transitionY = centerY - size / 2;

        canvas.save();
        canvas.translate(x, transitionY);
        drawable.draw(canvas);
        canvas.restore();
    }
}
